﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using FriendsNearby.Services.Entities;
using InSTEDD.Labs.Geo;
using FriendsNearby.Services.Facebook;
using Facebook.Service;
using Moq;

namespace FriendsNearby.Services.Tests
{
    [TestFixture]
    public class NearbyNotificationsFixture
    {
        Mock<ISocialNetworkService> socialNetworkService;
        Mock<IUsersService> usersService;
        IPeopleNearbyService peopleNearbyService;
        ILocationService locationService;
		Guid me = Guid.Empty;
		Guid b1 = Guid.NewGuid();

        [SetUp]
        public void SetUp()
        {
            socialNetworkService = new Mock<ISocialNetworkService>();
            usersService = new Mock<IUsersService>();
            DataAccess da = new DataAccess();
            LocationService loc = new LocationService(da);
            peopleNearbyService = new PeopleNearbyService(loc, usersService.Object, socialNetworkService.Object);
            loc.SetPeopleNearbyService(peopleNearbyService);
            locationService = loc;
        }

       
        [Test]
        public void NewFriendNearbyNotified()
        {
            var locationService = new Mock<ILocationService>();

            LatLong myLatLongBefore = new LatLong(0, 0);
            LatLong myLatLongNow = new LatLong(0, 5);
            LatLong friendLatLongBefore = new LatLong(0, 6);
            LatLong friendLatLongNow = new LatLong(0, 6);

            locationService.Expect(x => x.SetCurrentLocation(me, myLatLongNow)); 
            locationService.Expect(x => x.SetCurrentLocation(me, myLatLongBefore));
            locationService.Expect(x => x.SetCurrentLocation(b1, friendLatLongNow));
            locationService.Expect(x => x.SetCurrentLocation(b1, friendLatLongBefore));


            LocationInfo userInfoNow = new LocationInfo() { Location = myLatLongNow, UserID = me };
            LocationInfo userInfoBefore = new LocationInfo() { Location = myLatLongBefore, UserID = me };
            LocationInfo friendInfoNow = new LocationInfo() { Location = friendLatLongNow, UserID = b1 };
            LocationInfo friendInfoBefore = new LocationInfo() { Location = friendLatLongBefore, UserID = b1 };

            locationService.Expect(x => x.GetCurrentLocation(me)).Returns(userInfoNow);
            locationService.Expect(x => x.GetLocationAt(me, DateTime.Today)).Returns(userInfoBefore);
            locationService.Expect(x => x.GetCurrentLocation(b1)).Returns(friendInfoNow);
            locationService.Expect(x => x.GetLocationAt(b1, DateTime.Today)).Returns(friendInfoBefore);

            locationService.Expect(x => x.GetLastNotificationDate(me)).Returns(DateTime.Today);

			socialNetworkService.Expect(x=>x.GetFriends(me)).Returns(Utils.List<Guid>(b1));
			socialNetworkService.Expect(x => x.GetFriendsOfFriends(me)).Returns(new List<Guid>());

            var nearbyDistance = LatLong.DistanceInKm(new LatLong(0, 0), new LatLong(0, 2));

			usersService.Expect(x => x.GetUserPreferences(me)).Returns(new UserRegistry { UserID = me, NearbyRadiusKM = (int)nearbyDistance, VisibilityLevel = (int)VisibilityLevel.Everyone });
			usersService.Expect(x => x.GetUserPreferences(b1)).Returns(new UserRegistry { UserID = b1, NearbyRadiusKM = (int)nearbyDistance, VisibilityLevel = (int)VisibilityLevel.Everyone });

            var notifications = peopleNearbyService.GetNotificationsForUser(me);
            
            foreach (var item in notifications)
            {
                Console.Out.WriteLine(item);
            }
        }
    }
}
